package com.baichang.taoshunda.controller.mobile;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;

import com.baichang.taoshunda.service.IdleGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * Created by ${wushenglu} on 2018/1/17.
 */
@Controller
@RequestMapping(ControllerConstants.IDLEGOODS)
public class IdleGoodsCtroller extends BaseMobileController {

    @Autowired
    private IdleGoodsService idlegoodsService;

    /**
     * 添加闲置物品
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addIdleGoods")
    public void addIdleGoods(final HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                String name = getString(s, "name", "");
                String sunmmary = getString(s, "summary", "");//简介
                String picture = getString(s, "picture", "");
                BigDecimal newPrice = BigDecimal.valueOf(getDouble(s, "newPrice", 0));
                BigDecimal oldPrice = BigDecimal.valueOf(getDouble(s, "oldPrice", 0));
                Integer cityId = getInt(s, "cityId", 0);
                Integer areaId = getInt(s, "areaId", 0);
                Integer userId = getInt(s, "userId", 0);
                Integer id = getInt(s, "id", 0);
                String phone = getString(s, "phone", "");
                return idlegoodsService.addIdleGoods(name, sunmmary, picture, newPrice, oldPrice, cityId, areaId, userId, phone,id);
            }
        });
    }

    /**
     * 查询闲置物品
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryIdleGoods")
    public void queryIdleGoods(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                Integer cityId = getInt(json, "cityId", 0);
                Integer areaId = getInt(json, "areaId", 0);
                String name = getString(json, "name", "");
                int nowPage = getInt(json, "nowPage", 1);
                int pageSize = getInt(json, "pageSize", 20);
                return idlegoodsService.queryIdleGoods(userId, cityId, areaId, name, nowPage, pageSize);
            }
        });
    }

    /**
     * 查看闲置详细
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryIdleGoodsDetails")
    public void queryIdleGoodsDetails(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer idleId = getInt(json, "id", 0);
                Integer userId = getInt(json, "userId", 0);
                return idlegoodsService.queryIdleGoodsDetails(idleId, userId);
            }
        });
    }

    /**
     * 收藏闲置商品
     *
     * @param request
     * @param response
     */
    @RequestMapping("/idleCollection")
    public void idleCollection(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer idleId = getInt(json, "idleId", 0);
                Integer userId = getInt(json, "userId", 0);
                return idlegoodsService.addIdleCollection(idleId, userId);
            }
        });
    }

    /**
     * 删除闲置商品
     *
     * @param request
     * @param response
     */
    @RequestMapping("/deleteIdle")
    public void deleteIdle(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer idleId = getInt(json, "idleId", 0);
                return idlegoodsService.deleteIdle(idleId);
            }
        });
    }

    /**
     * 查询收藏列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findIdleCollection")
    public void findIdleCollection(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer userId = getInt(json, "userId", 0);
                Integer nowPage = getInt(json, "nowPage", 1);
                return idlegoodsService.findIdleCollection(userId, nowPage, 20);
            }
        });
    }
}
